version 14.0
clear all
set more off
#delimit;	

global strformat "%8.3f";

capture program drop sig_p;
program sig_p;
   args obj_var point_est_var point_sd_var p_val row_id_var row_num;
   local point_est `point_est_var';
   local point_sd `point_sd_var';
   if `p_val' > 0.1 {;
      replace `obj_var' = string(`point_est', "$strformat") if `row_id_var' == `row_num';
      };
   if `p_val' > 0.05 & `p_val' <= 0.1  {;
      replace `obj_var' = string(`point_est', "$strformat")+"*" if `row_id_var' == `row_num';
      };
   if `p_val' > 0.01 & `p_val' <= 0.05  {;
      replace `obj_var' = string(`point_est', "$strformat")+"**" if `row_id_var' == `row_num';
      };
   if `p_val' <= 0.01 {;
      replace `obj_var' = string(`point_est', "$strformat")+"***" if `row_id_var' == `row_num';
      };
   replace `obj_var' = "<" + string(`point_sd', "$strformat") + ">" + " " if `row_id_var' == `row_num' + 1;
   end;
	
	
*enter directory name here;

use "$dir/ReStat_children.dta", clear;

keep if fu_child_level == 1 & fu_young_child == 1;
  
	
forvalues i=1/15 {;
	gen item`i' = qu`i';
};
	
forvalues i=1/24 {;
	local j = `i' + 15;
	gen item`j' = qm`i';
};

	
alpha item1-item39, item;
	
egen sumscore = rowtotal(item1-item39);
	
forvalues i = 1/39 {;
	egen eicc`i' = mean(item`i'), by(sumscore);
};
		
		
egen weight = count(sumscore), by(sumscore);

	
irt 2pl item1-item39;
estimates store mcas1pl;
	
estat report, sort(b) byparm;

predict theta1pl_total, latent se(the1plse_total);
	
	
irt 1pl qm1-qm24;
predict theta1pl_math, latent se(the1plse_math);

irt 1pl qu1-qu15;
predict theta1pl_urdu, latent se(the1plse_urdu);

	
reg theta1pl_total pooled_treatment [pw=hh_weight], cluster(villcode);

	
drop if villcode == . | hhid == . | cid == .;
forvalues i=1/39 {;
	drop if item`i' == .;
};
gen unique_id = _n;
	
gen theta = rnormal(0,1);
	
preserve;
keep unique_id item*;
	
openirt, id(unique_id) save_item_parameters("items.dta") save_trait_parameters("traits.dta") item_prefix(item) model("2PL");
	
restore;
	
rename unique_id id;
merge 1:1 id using "traits.dta", gen(merge_traits);
	
	


gen row_num = _n;
	
forvalues i=0/25 {;
	generate col`i' = "";
};

local row = 4;
sort row_num;
order col0-col25;

su theta_mle if pooled_treatment == 0;
gen sd_theta_mle = r(sd);
gen mean_theta_mle = r(mean);

gen z_theta_mle = (theta_mle - mean_theta_mle)/sd_theta_mle;


su theta_eap if pooled_treatment == 0;
gen sd_theta_eap = r(sd);
gen mean_theta_eap = r(mean);

gen z_theta_eap = (theta_eap - mean_theta_eap)/sd_theta_eap;

foreach var of varlist 
	z_theta_eap z_theta_mle {;
	
	replace col0 = "`var'" if row_num == `row';
	
	su `var' if pooled_treatment == 0 & fu_child_level == 1 & fu_young_child == 1 [aw=hh_weight];
	replace col2 = string(r(mean), "$strformat") if row_num == `row';	
	replace col2 = string(r(sd), "$strformat") if row_num == `row'+1;	
	
	regress `var' pooled_treatment 
		if fu_child_level == 1 & fu_young_child == 1 [pw=hh_weight], cluster(villcode);
	test pooled_treatment;
	sig_p col4 _b[pooled_treatment] _se[pooled_treatment] r(p) row_num `row';

	regress `var' pooled_treatment 
		$fu_child_controls_cm 
		if fu_child_level == 1 & fu_young_child == 1 [pw=hh_weight], cluster(villcode);
	test pooled_treatment;
	sig_p col5 _b[pooled_treatment] _se[pooled_treatment] r(p) row_num `row';
	
	regress `var' pooled_treatment 
		$fu_child_controls_cm $fu_hh_controls_cm 
		if fu_child_level == 1 & fu_young_child == 1 [pw=hh_weight], cluster(villcode);
	test pooled_treatment;
	sig_p col6 _b[pooled_treatment] _se[pooled_treatment] r(p) row_num `row';
	
	xi: regress `var' pooled_treatment 
		$fu_child_controls_cm $fu_hh_controls_cm i.bl_district 
		if fu_child_level == 1 & fu_young_child == 1 [pw=hh_weight], cluster(villcode);
	test pooled_treatment;
	sig_p col7 _b[pooled_treatment] _se[pooled_treatment] r(p) row_num `row';
	
	
	xi: ivreg2 `var' (fu_child_enrolled = pooled_treatment) 
		$fu_child_controls_cm $fu_hh_controls_cm i.bl_district 
		if fu_child_level == 1 & fu_young_child == 1 [pw=hh_weight], cluster(villcode);
	test fu_child_enrolled;
	sig_p col9 _b[fu_child_enrolled] _se[fu_child_enrolled] r(p) row_num `row';
	
	
	local row = `row' + 3;

};


local row = `row' + 1;




outsheet col0-col9 using "$dir/Table B2 test scores (theta parameter).csv" if
	row_num < `row', comma replace;
drop col*;
